home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / canswers.zip / CANSWERS.DOC next >
Text File  |  1990-08-21  |  21KB  |  559 lines

  1. Article 29029 (151 more) in comp.lang.c:
  2. From: scs@adam.mit.edu (Steve Summit)
  3. Subject: Answers to Frequently Asked Questions (FAQ) on comp.lang.c (Abridged)
  4. Summary: Short answers for those who don't feel like reading the longer explanations
  5. Message-ID: <1990Aug15.040348.25120@athena.mit.edu>
  6. Date: 15 Aug 90 04:03:48 GMT
  7. Sender: daemon@athena.mit.edu (Mr Background)
  8. Reply-To: scs@adam.mit.edu (Steve Summit)
  9. Organization: Thermal Technologies, Inc.
  10. Lines: 546
  11. Supersedes: <1990Aug1.042401.20373@athena.mit.edu>
  12.  
  13. This article contains minimal answers to the comp.lang.c frequently
  14. asked questions list.  Please see the long version (posted on the
  15. first of each month) for more detailed explanations.
  16.  
  17. Null Pointers
  18.  
  19. 1.  What is this infamous null pointer, anyway?
  20.  
  21. A:  For each pointer type, there is a special value -- the "null
  22.     pointer" -- which is distinguishable from all other pointer values
  23.     and which is not the address of any object.
  24.  
  25.     References: K&R I Sec. 5.4 pp. 97-8; K&R II Sec. 5.4 p. 102; H&S
  26.     Sec. 5.3 p. 91; ANSI X3.159-1989 Sec. 3.2.2.3 .
  27.  
  28. 2.  How do I "get" a null pointer in my programs?
  29.  
  30. A:  A constant 0 in a pointer context is converted into a null pointer
  31.     at compile time.  A "pointer context" is an initialization,
  32.     assignment, or comparison with one side a variable of pointer type,
  33.     and (in ANSI standard C) a function argument which has a prototype
  34.     in scope declaring a certain parameter as being of pointer type.  In
  35.     other contexts (function arguments without prototypes, or in the
  36.     variable part of variadic functions) a constant 0 with an
  37.     appropriate explicit cast is required.
  38.  
  39.     References: K&R I Sec. A7.7 p. 190, Sec. A7.14 p. 192; K&R II Sec.
  40.     A7.10 p. 207, Sec. A7.17 p. 209; H&S Sec. 4.6.3 p. 72; ANSI X3.159-
  41.     1989 Sec. 3.2.2.3 .
  42.  
  43. 3.  But aren't pointers the same as ints?
  44.  
  45. A:  Not since the early days.
  46.  
  47.     References: K&R I Sec. 5.6 pp. 102-3; ANSI X3.159-1989 Sec. 3.3.4 .
  48.  
  49. 4.  What is NULL and how is it #defined?
  50.  
  51. A:  NULL is simply a preprocessor macro, #defined as 0 (or (void *)0)
  52.     which is used (as a stylistic convention, in favor of unadorned 0's)
  53.     to generate null pointers,
  54.  
  55.     References: K&R I Sec. 5.4 pp. 97-8; K&R II Sec. 5.4 p. 102; H&S
  56.     Sec. 13.1 p. 283; ANSI X3.159-1989 Sec. 4.1.5 p. 99, Sec. 3.2.2.3
  57.     p. 38, Rationale Sec. 4.1.5 p. 74.
  58.  
  59. 5.  How should NULL be #defined on a machine which uses a nonzero bit
  60.     pattern as the internal representation of a null pointer?
  61.  
  62. A:  The same as any other machine: as 0 (or (void *)0).  (The compiler
  63.     makes the translation, upon seeing a 0, not the preprocessor.)
  64.  
  65. 6.  If NULL were defined as "(char *)0," wouldn't that make function
  66.     calls which pass an uncast NULL work?
  67.  
  68. A:  Not in general.  The problem is that there are machines which use
  69.     different internal representations for pointers to different types
  70.     of data.  A cast is still required to tell the compiler which kind
  71.     of null pointer is required, since it may be different from (char
  72.     *)0.
  73.  
  74. 7.  Is the abbreviated pointer comparison "if(p)" to test for non-null
  75.     pointers valid?  What if the internal representation for null
  76.     pointers is nonzero?
  77.  
  78. A:  The construction "if(p)" works, regardless of the internal
  79.     representation of null pointers, because the compiler essentially
  80.     rewrites it as "if(p != 0)" and goes on to convert 0 into the
  81.     correct null pointer.
  82.  
  83.     References: K&R II Sec. A7.4.7 p. 204; H&S Sec. 5.3 p. 91; ANSI
  84.     X3.159-1989 Secs. 3.3.3.3, 3.3.9, 3.3.13, 3.3.14, 3.3.15, 3.6.4.1,
  85.     and 3.6.5 .
  86.  
  87. 8.  If "NULL" and "0" are equivalent, which should I use?
  88.  
  89. A:  Either; the distinction is entirely stylistic.
  90.  
  91.     References: K&R II Sec. 5.4 p. 102.
  92.  
  93. 9.  But wouldn't it be better to use NULL (rather than 0) in case the
  94.     value of NULL changes, perhaps on a machine with nonzero null
  95.     pointers?
  96.  
  97. A:  No.  NULL is, and will always be, 0.
  98.  
  99. 10. But I once used a compiler that wouldn't work unless NULL was used.
  100.  
  101. A:  This compiler was broken.
  102.  
  103. 11. I'm confused.  NULL is guaranteed to be 0, but the null pointer is
  104.     not?
  105.  
  106. A:  A "null pointer" (written in lower case in this article) is a
  107.     language concept whose particular internal value does not matter.  A
  108.     "null pointer" is requested in source code with the character "0".
  109.     "NULL" (always in capital letters) is a preprocessor macro, which is
  110.     always #defined as 0 (or (void *)0).
  111.  
  112. 12. Why is there so much confusion surrounding null pointers?  Why do
  113.     these questions come up so often?
  114.  
  115. A:  The fact that null pointers are represented both in source code, and
  116.     internally to most machines, as zero invites unwarranted
  117.     assumptions.  The fact that a preprocessor macro (NULL) is often
  118.     used suggests that this is done because the value might change
  119.     later, or on some weird machine.
  120.  
  121. 13. I'm still confused.  I just can't understand all this null pointer
  122.     stuff.
  123.  
  124. A:  A simple rule is, "Always use `0' or `NULL' for null pointers, and
  125.     always cast them when they are used in function calls."
  126.  
  127. Arrays and Pointers
  128.  
  129. 14. I had the declaration char a[5] in one source file, and in another I
  130.     declared extern char *a.  Why didn't it work?
  131.  
  132. A:  The declaration extern char *a simply does not match the actual
  133.     definition.   Use extern char a[].
  134.  
  135. 15. But I heard that char a[] was identical to char *a.
  136.  
  137. A:  This identity (that a pointer declaration is interchangeable with an
  138.     array declaration, usually unsized) holds _only_ for formal
  139.     parameters to functions.  Otherwise, the two forms are not
  140.     interchangeable.
  141.  
  142.     References: K&R I Sec. 5.3 p. 95, Sec. A10.1 p. 205; K&R II Sec. 5.3
  143.     p. 100, Sec. A8.6.3 p. 218, Sec. A10.1 p. 226; H&S Sec. 5.4.3 p. 96;
  144.     ANSI X3.159-1989 Sec. 3.5.4.3, Sec. 3.7.1 .
  145.  
  146. 16. So what is meant by the "equivalence of pointers and arrays" in C?
  147.  
  148. A:  Saying that arrays and pointers are "equivalent" does not by any
  149.     means imply that they are interchangeable.  "Equivalence" refers to
  150.     the fact that arrays decay into pointers within expressions, and
  151.     that pointers and arrays can both be dereferenced using array-like
  152.     subscript notation.
  153.  
  154.     References: K&R I Sec. 5.3 pp. 93-6; K&R II Sec. 5.3 p. 99; H&S Sec.
  155.     5.4.1 p. 93; ANSI X3.159-1989 Sec. 3.3.2.1, Sec. 3.3.6 .
  156.  
  157. 17. My compiler complained when I passed a two-dimensional array to a
  158.     routine expecting a pointer to a pointer.
  159.  
  160. A:  The rule by which arrays decay into pointers is not applied
  161.     recursively.  An array of arrays (i.e. a two-dimensional array in C)
  162.     decays into a pointer to an array, not a pointer to a pointer.
  163.  
  164. 18. How do I declare a pointer to an array?
  165.  
  166. A:  Usually, you don't want one.  Think about using a pointer to one of
  167.     the array's elements instead.
  168.  
  169. 19. How can I dynamically allocate a multidimensional array?
  170.  
  171. A:  It is usually best to allocate an array of pointers, and then
  172.     initialize each pointer to a dynamically-allocated "row." See the
  173.     full list for code samples.
  174.  
  175. Order of Evaluation
  176.  
  177. 20. Under my compiler, the code "int i = 7; printf("%d\n", i++ * i++); "
  178.     prints 49.  Regardless of the order of evaluation, shouldn't it
  179.     print 56?
  180.  
  181. A:  The operations implied by the postincrement and postdecrement
  182.     operators ++ and -- are performed at some time after the operand's
  183.     former values are yielded and before the end of the expression, but
  184.     not necessarily immediately after, or before other parts of the
  185.     expression are evaluated.
  186.  
  187.     References: K&R I Sec. 2.12 p. 50; K&R II Sec. 2.12 p. 54; ANSI
  188.     X3.159-1989 Sec. 3.3 .
  189.  
  190. 21. But what about the &&, ||, ?:, and comma operators?
  191.  
  192. A:  There is a special exception for those operators; left-to-right
  193.     evaluation is guaranteed.
  194.  
  195.     References: ANSI X3.159-1989 Secs. 3.3.2.2, 3.3.13, 3.3.14, 3.3.15 .
  196.  
  197. ANSI C
  198.  
  199. 22. What is the "ANSI C Standard?"
  200.  
  201. A:  In 1983, the American National Standards Institute commissioned a
  202.     committee, X3J11, to standardize the C language.  After a long and
  203.     arduous process, this C standard was finally ratified as an American
  204.     National Standard, X3.159-1989, on December 14, 1989, and published
  205.     in the spring of 1990.
  206.  
  207. 23. How can I get a copy of the ANSI C standard?
  208.  
  209. A:  Copies are available from the American National Standards Institute
  210.     in New York, or from Global Engineering Documents in Irvine, CA.
  211.     See the unabridged list for full addresses.
  212.  
  213. 24. Does anyone have a tool for converting old-style C programs to ANSI
  214.     C, or for automatically generating prototypes?
  215.  
  216. A:  There are several such programs, many in the public domain.  See the
  217.     full list for details.
  218.  
  219. 25. My ANSI compiler complains about a mismatch when it sees
  220.  
  221.          extern int func(float);
  222.          int func(x)
  223.          float x;
  224.          {...
  225.  
  226. A:  You have mixed the new-style declaration "extern int func(float);"
  227.     with the old-style definition "int func(x) float x;".  The problem
  228.     can be fixed either by using new-style syntax consistently, or by
  229.     changing the new-style prototype declaration to match the old-style
  230.     definition.
  231.  
  232.     References: ANSI X3.159-1989 Sec. 3.3.2.2 .
  233.  
  234. C Preprocessor
  235.  
  236. 26. How can I write a macro to swap two values?
  237.  
  238. A:  There is no good answer to this question.  The best all-around
  239.     solution is probably to forget about using a macro.
  240.  
  241. 27. I'm getting strange syntax errors inside code which I've #ifdeffed
  242.     out.
  243.  
  244. A:  Under ANSI C, #ifdeffed-out text must still consist of "valid
  245.     preprocessing tokens."  This means that there must be no
  246.     unterminated comments or quotes and no newlines inside quotes.
  247.  
  248. 28. How can I write a cpp macro which takes a variable number of
  249.     arguments?
  250.  
  251.     One popular trick is to define the macro with a single argument, and
  252.     call it with a double set of parentheses, which appear to the
  253.     compiler to indicate a single argument:
  254.  
  255.          #define DEBUG(args) {printf("DEBUG: ");printf args;}
  256.  
  257.          if(n != 0) DEBUG(("n is %d\n", n));
  258.  
  259. Variable-Length Argument Lists
  260.  
  261. 29. How can I write a function that takes a variable number of
  262.     arguments?
  263.  
  264. A:  Use varargs or stdarg.
  265.  
  266.     References: K&R II Sec. 7.3 p. 155, Sec. B7 p. 254; H&S Sec. 13.4
  267.     pp. 286-9; ANSI X3.159-1989 Secs. 4.8 through 4.8.1.3 .
  268.  
  269. 30. How can I write a function that takes a format string and a variable
  270.     number of arguments, like printf, and passes them to printf to do
  271.     most of the work?
  272.  
  273. A:  Use vprintf, vfprintf, or vsprintf.
  274.  
  275.     References: K&R II Sec. 8.3 p. 174, Sec. B1.2 p. 245; H&S Sec. 17.12
  276.     p. 337; ANSI X3.159-1989 Secs. 4.9.6.7, 4.9.6.8, 4.9.6.9 .
  277.  
  278. 31. How can I write a function analogous to scanf?
  279.  
  280. A:  Unfortunately, vscanf and the like are not standard.
  281.  
  282. 32. How can I discover how many arguments a function was actually called
  283.     with?
  284.  
  285. A:  This information is not available to a portable program.  Any
  286.     function which takes a variable number of arguments must be able to
  287.     determine from the arguments themselves how many of them there are.
  288.  
  289. 33. How can I write a function which takes a variable number of
  290.     arguments and passes them to some other function (which takes a
  291.     variable number of arguments)?
  292.  
  293. A:  In general, you cannot.
  294.  
  295. Memory Allocation
  296.  
  297. 34. Why doesn't the code "char *answer; gets(answer);" work?
  298.  
  299. A:  The pointer variable "answer" has not been set to point to any valid
  300.     storage.  The simplest way to correct this fragment is to use a
  301.     local array, instead of a pointer.
  302.  
  303. 35. You can't use dynamically-allocated memory after you free it, can
  304.     you?
  305.  
  306. A:  No.  Some early man pages implied otherwise, but the claim is no
  307.     longer valid.
  308.  
  309. 36. What is alloca and why is its use discouraged?
  310.  
  311. A:  alloca allocates memory which is automatically freed when the
  312.     function from which alloca was called returns.  alloca cannot be
  313.     written portably, is difficult to implement on machines without a
  314.     stack, and fails under certain conditions if implemented simply.  It
  315.     cannot be used in programs which must be widely portable.
  316.  
  317. Structures
  318.  
  319. 37. I heard that structures could be assigned to variables and passed to
  320.     and from functions, but K&R I says not.
  321.  
  322. A:  These operations are supported by all modern compilers.
  323.  
  324.     References: K&R I Sec. 6.2 p. 121; K&R II Sec. 6.2 p. 129; H&S Sec.
  325.     5.6.2 p. 103; ANSI X3.159-1989 Secs. 3.1.2.5, 3.2.2.1, 3.3.16 .
  326.  
  327. 38. How does struct passing and returning work?
  328.  
  329. A:  If you really need to know, see the unabridged list.
  330.  
  331. 39. I have a program which works correctly, but it dumps core after it
  332.     finishes.  Why?
  333.  
  334. A:  Check to see if a structure declaration just before main is missing
  335.     its trailing semicolon, causing the compiler to believe that main
  336.     returns a struct.
  337.  
  338. 40. Why can't you compare structs?
  339.  
  340. A:  There is no reasonable way for a compiler to implement struct
  341.     comparison which is consistent with C's low-level flavor.
  342.  
  343.     References: K&R II Sec. 6.2 p. 129; H&S Sec. 5.6.2 p. 103.
  344.  
  345. 41. How can I determine the byte offset of a field within a structure?
  346.  
  347. A:  ANSI C defines the offsetof macro, which should be used if
  348.     available.
  349.  
  350.     References: ANSI X3.159-1989 Sec. 4.1.5 .
  351.  
  352. 42. How can I access structure fields by name at run time?
  353.  
  354. A:  Build a table of names and offsets, using the offsetof() macro.
  355.  
  356. Declarations
  357.  
  358. 43. I can't seem to define a linked list node which contains a pointer
  359.     to itself.
  360.  
  361. A:  Structs in C can certainly contain pointers to themselves; the
  362.     discussion and example in section 6.5 of K&R make this clear.
  363.     Problems arise if an attempt is made to define (and use) a typedef
  364.     in the midst of such a declaration; avoid this.
  365.  
  366.     References: K&R I Sec. 6.5 p. 101; K&R II Sec. 6.5 p. 139; H&S Sec.
  367.     5.6.1 p. 102; ANSI X3.159-1989 Sec. 3.5.2.3 .
  368.  
  369. 44. How can I define a pair of mutually referential structures?
  370.  
  371. A:  The obvious technique works as long as any typedef synonyms are
  372.     defined outside of the struct declarations.
  373.  
  374.     References: H&S Sec. 5.6.1 p. 102; ANSI X3.159-1989 Sec. 3.5.2.3 .
  375.  
  376. 45. How do I declare a pointer to a function returning a pointer to a
  377.     double?
  378.  
  379. A:  double *(*p)();
  380.     Using a chain of typedefs, or the cdecl program, makes these
  381.     declarations easier.
  382.  
  383.     References: H&S Sec. 5.10.1 p. 116.
  384.  
  385. 46. So where can I get cdecl?
  386.  
  387. A:  Several public-domain versions are available.  See the full list for
  388.     details.
  389.  
  390. Boolean Expressions and Variables
  391.  
  392. 47. What is the right type to use for boolean values in C?  Why isn't it
  393.     a standard type?  Should #defines or enums be used for the true and
  394.     false values?
  395.  
  396. A:  C does not provide a standard boolean type, because picking one
  397.     involves a space/time tradeoff which is best decided by the
  398.     programmer.  The choice between #defines and enums is arbitrary and
  399.     not terribly interesting.
  400.  
  401. 48. Isn't #defining TRUE to be 1 dangerous, since any nonzero value is
  402.     considered "true" in C?  What if a built-in boolean or relational
  403.     operator "returns" something other than 1?
  404.  
  405. A:  It is true (sic) that any nonzero value is considered true in C, but
  406.     this applies only "on input", i.e. where a boolean value is
  407.     expected.  When a boolean value is generated by a built-in operator,
  408.     it is guaranteed to be 1 or 0.  (This is _not_ true for some library
  409.     routines such as isalpha.)
  410.  
  411.     References: K&R I Sec. 2.7 p. 41; K&R II Sec. 2.6 p. 42, Sec. A7.4.7
  412.     p. 204, Sec. A7.9 p. 206; ANSI X3.159-1989 Secs. 3.3.3.3, 3.3.8,
  413.     3.3.9, 3.3.13, 3.3.14, 3.3.15, 3.6.4.1, 3.6.5 .
  414.  
  415. 49. What is the difference between an enum and a series of preprocessor
  416.     #defines?
  417.  
  418. A:  At the present time, there is little difference.  The ANSI standard
  419.     states that enums are compatible with ints.
  420.  
  421.     References: K&R II Sec. 2.3 p. 39, Sec. A4.2 p. 196; H&S Sec. 5.5
  422.     p. 100; ANSI X3.159-1989 Secs. 3.1.2.5, 3.5.2, 3.5.2.2 .
  423.  
  424. Operating System Dependencies
  425.  
  426. 50. How can I read a single character from the keyboard without waiting
  427.     for a newline?
  428.  
  429. A:  Contrary to popular belief and many people's wishes, this is not a
  430.     C-related question.  How to do so is a function of the operating
  431.     system in use.
  432.  
  433. 51. How can I find out if there are characters available for reading
  434.     (and if so, how many)?  Alternatively, how can I do a read that will
  435.     not block if there are no characters available?
  436.  
  437. A:  These, too, are entirely operating-system-specific.
  438.  
  439. 52. How can my program discover the complete pathname to the executable
  440.     file from which it was invoked?
  441.  
  442. A:  argv[0] may contain all or part of the pathname.  You may be able to
  443.     duplicate the command language interpreter's search path logic to
  444.     locate the executable.
  445.  
  446. 53. How can a process change an environment variable in its caller?
  447.  
  448. A:  In general, it cannot.
  449.  
  450. Stdio
  451.  
  452. 54. Why does errno contain ENOTTY after a call to printf?
  453.  
  454. A:  Don't worry about it.  It is only meaningful for a program to
  455.     inspect the contents of errno after an error has occurred.
  456.  
  457. 55. My program's prompts and intermediate output don't always show up on
  458.     my screen, especially when I pipe the output through another
  459.     program.
  460.  
  461. A:  It is best to use an explicit fflush(stdout) at any point within
  462.     your program at which output should definitely be visible.
  463.  
  464. 56. When I read from the keyboard with scanf(), it seems to hang until I
  465.     type one extra line of input.
  466.  
  467. A:  scanf() was designed for free-format input, which is seldom what you
  468.     want when reading from the keyboard.
  469.  
  470. 57. So what should I use instead?
  471.  
  472. A:  Use fgets() to read a whole line, and then use sscanf() or other
  473.     string functions to parse the line buffer.
  474.  
  475. Miscellaneous
  476.  
  477. 58. Can someone tell me how to write itoa?
  478.  
  479. A:  Just use sprintf.
  480.  
  481. 59. How can I convert a struct tm or a string into a time_t?
  482.  
  483. A:  The ANSI mktime routine converts a struct tm to a time_t.  No
  484.     standard routine exists to parse strings.
  485.  
  486.     References: K&R II Sec. B10 p. 256; H&S Sec. 20.4 p. 361; ANSI
  487.     X3.159-1989 Sec. 4.12.2.3 .
  488.  
  489. 60. I seem to be missing the system header file <sgtty.h>.  Can someone
  490.     send me a copy?
  491.  
  492. A:  You cannot just pick up a copy of someone else's header file and
  493.     expect it to work, since the definitions within header files are
  494.     frequently system-dependent.  Contact your vendor.
  495.  
  496. 61. Does anyone know of a program for converting Pascal (Fortran, lisp,
  497.     "Old" C, ...) to C?
  498.  
  499. A:  Several public-domain programs are available, namely ptoc, p2c, and
  500.     f2c.  See the full list for details.
  501.  
  502. 62. Where can I get copies of all these public-domain programs?
  503.  
  504. A:  See the regular postings in the comp.sources.unix and
  505.     comp.sources.misc newsgroups.
  506.  
  507. 63. How can I call Fortran (BASIC, Pascal, ADA, LISP) functions from C?
  508.  
  509. A:  The answer is entirely dependent on the machine and the specific
  510.     calling sequences of the various compilers in use.
  511.  
  512. 64. Why don't C comments nest?  Are they legal inside quoted strings?
  513.  
  514. A:  Nested comments would cause more harm than good.  The character
  515.     sequences /* and */ are not special within double-quoted strings.
  516.  
  517. 65. I'm having trouble with a Turbo C program which crashes and says
  518.     something like "floating point not loaded."
  519.  
  520. A:  Some compilers for small machines, including Turbo C and Ritchie's
  521.     original pdp11 compiler, attempt to leave out floating point support
  522.     if it looks like it will not be needed.  The programmer must
  523.     occasionally insert one dummy explicit floating-point operation to
  524.     force loading of floating-point support.
  525.  
  526. 66. Does anyone have a C compiler test suite I can use?
  527.  
  528. A:  Plum Hall, among others, sells one.
  529.  
  530. 67. Where can I get a YACC grammar for C?
  531.  
  532. A:  See the unabridged list.
  533.  
  534. 68. Where can I get the "Indian Hill Style Guide" and other coding
  535.     standards?
  536.  
  537. A:  See the unabridged list.
  538.  
  539. 69. Where can I get extra copies of this list?
  540.  
  541. A:  For now, just pull it off the net; it is normally posted on about
  542.     the first of the month, with an Expiration: line which should keep
  543.     it around all month.
  544.  
  545.  
  546. Thanks to Mark Brader, Joe Buehler, Christopher Calabrese, Stephen M.
  547. Dunn, Tony Hansen, Guy Harris, Karl Heuer, Blair Houghton, Kirk Johnson,
  548. Andrew Koenig, John Lauro, Christopher Lott, Rich Salz, Joshua Simons,
  549. and Erik Talvola, who have contributed, directly or indirectly, to this
  550. article.
  551.  
  552.                                              Steve Summit
  553.                                              scs@adam.mit.edu
  554.                                              scs%adam.mit.edu@mit.edu
  555.  
  556. This article is Copyright 1988, 1990 by Steve Summit.
  557. It may be freely redistributed so long as the author's name, and this
  558. notice, are retained.
  559.